package com.woniu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.CustomerInfos;
import com.woniu.service.ICustomerInfosService;
import com.woniu.service.ISalesService;
import com.woniu.util.ResultVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2021-03-29
 */
@RestController
public class CustomerInfosController {

    @Autowired
    private ICustomerInfosService customerInfosService;

    @Autowired
    private ISalesService salesService;

    @PostMapping ("/customer/save")
    public ResultVO addCustomerInfo(@RequestBody CustomerInfos customerInfos) {
        if (customerInfos == null) {
            ResultVO.failureForNull();
        }
        boolean save = false;
        try {
            save = customerInfosService.save(customerInfos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return save ? ResultVO.success() : ResultVO.failure();
    }

    @PostMapping ("/customer/update")
    public ResultVO EditCustomerInfo(@RequestBody CustomerInfos customerInfos) {
        if (customerInfos == null) {
            ResultVO.failureForNull();
        }
        boolean updateById = false;
        try {
            updateById = customerInfosService.updateById(customerInfos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return updateById ? ResultVO.success() : ResultVO.failure();
    }

    @PostMapping ("/customer/selectAllCustomer")
    public ResultVO GetAllCustomer() {
        List<CustomerInfos> customerInfosList = customerInfosService.selectAllCustomer();
        if (customerInfosList == null) {
            ResultVO.failureForNull();
        }
        return customerInfosList != null ? ResultVO.success(customerInfosList) : ResultVO.failure();
    }

    @PostMapping ("/customer/selectById")
    public ResultVO GetCustomerInfoById(@RequestBody CustomerInfos customerInfos) {
        if (customerInfos == null) {
            ResultVO.failureForNull();
        }
        CustomerInfos customerInfos1 = null;
        try {
            customerInfos1 = customerInfosService.getBaseMapper().selectById(customerInfos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customerInfos1 != null ? ResultVO.success(customerInfos1) : ResultVO.failure();
    }

    @PostMapping ("/customer/selectByUserId")
    public ResultVO GetCustomerInfoByUserId(@RequestBody CustomerInfos customerInfos) {

        if (customerInfos == null && customerInfos.getUserId() == null) {
            ResultVO.failureForNull();
        }
        List<CustomerInfos> customerInfosList = null;
        try {
            QueryWrapper<CustomerInfos> customerInfosQueryWrapper = new QueryWrapper<>();
            customerInfosQueryWrapper.like("User_ID", customerInfos.getUserId())
                    .like(StringUtils.isNoneEmpty(customerInfos.getCustomerName()), "Customer_Name", customerInfos.getCustomerName())
                    .like(StringUtils.isNotEmpty(customerInfos.getPhone()), "Phone", customerInfos.getPhone());
            customerInfosList = customerInfosService.getBaseMapper().selectList(customerInfosQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customerInfosList != null ? ResultVO.success(customerInfosList) : ResultVO.failure();
    }

    @PostMapping ("/customer/selectByNameAndUserId")
    public ResultVO GetCustomerInfoByNameAndUserId(@RequestBody CustomerInfos customerInfos) {
        if (customerInfos == null) {
            ResultVO.failureForNull();
        }
        List<CustomerInfos> customerInfosList = null;
        try {
            QueryWrapper<CustomerInfos> customerInfosQueryWrapper = new QueryWrapper<>();
            customerInfosQueryWrapper.like(StringUtils.isNotEmpty(customerInfos.getCustomerName()), "Customer_Name", customerInfos.getCustomerName()).
                    eq(StringUtils.isNotEmpty(customerInfos.getUserId() + ""), "User_ID", customerInfos.getUserId());

            customerInfosList = customerInfosService.getBaseMapper().selectList(customerInfosQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customerInfosList != null ? ResultVO.success(customerInfosList) : ResultVO.failure();
    }

    @PostMapping ("/customer/delete")
    public ResultVO DeleteCustomerInfo(@RequestBody CustomerInfos customerInfos) {
//        Integer customerId = customerInfos.getCustomerId();
        boolean delete = false;
        try {
//            ResultVO resultVO = salesService.findByCustomerID(customerId);
//            if (resultVO!=null){
                delete = customerInfosService.removeById(customerInfos);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return delete ? ResultVO.success() : ResultVO.failure();
    }
}

